var mongoose = require('../../../../../framework/mongoose');
var service = require('./service');

var CmsContactUs = require('./model').CmsContactUs;

//Rest Interface
exports.queryContactUsBySubId = function (req, res) {
    CmsContactUs.find({subscription: req.query.subId}).sort('order').exec(function (err, objs) {
        res.send({code: 1, data: {contactUs: objs[0]}});
    });
};
exports.findById = function(req, res){
    mongoose.findById(CmsContactUs, req.params.id, function(obj) {res.send({contactUs: obj});});
};

exports.save = function (req, res) {
    var contactUs = new CmsContactUs({
        name: req.body.name,
        image: req.body.image,
        address: req.body.address,
        email: req.body.email,
        telephone: req.body.telephone,
        website: req.body.website,

        subscription: req.body.subId,
        createDate: new Date()
    });
    contactUs.save(function(){
        return res.send({code: 1, data: coupon});
    });
};

exports.update = function(req, res){
    console.log(req.params.id);
    console.log(req.body);
    CmsContactUs.findByIdAndUpdate(
        req.params.id,
        {
            name: req.body.name,
            image: req.body.image,
            address: req.body.address,
            email: req.body.email,
            telephone: req.body.telephone,
            website: req.body.website,

            updateDate: new Date()
        }, function(){
            return res.send({code: 1});
        }
    )
};

exports.delete = function(req, res){
    CmsContactUs.findByIdAndRemove(req.params.id, function(){
        return res.send({code: 1});
    })
};